package com.qztc.news_end_core.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.qztc.news_end_dto.dto.News;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@RestController
@CrossOrigin
@RequestMapping("/news")
@Tag(name = "新闻接口")
public class NewsController {

    @Operation(summary = "展示所有新闻")
    @GetMapping("/show")
    public List<News> show() {
        return new News().selectAll();
    }

    @Operation(summary = "根据新闻id删除新闻")
    @DeleteMapping("/deletebynewsid")
    public boolean deleteByNewsId(@RequestBody News news) {
        UpdateWrapper<News> wrapper = new UpdateWrapper<>();
        wrapper.eq("newsid",news.getNewsid());
        return news.delete(wrapper);
    }

    @Operation(summary = "更新新闻")
    @PutMapping("/update")
    public boolean update(@RequestBody News news) {
        UpdateWrapper<News> wrapper = new UpdateWrapper<>();
        wrapper.eq("newsid",news.getNewsid());
        return news.update(wrapper);
    }

    @Operation(summary = "新增新闻")
    @PostMapping("/add")
    public boolean add(@RequestBody News news) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 a hh:mm:ss", Locale.CHINA);
        Date date = new Date();
        String time = sdf.format(date);
        news.setCreatedate(time);
        return news.insert();
    }

    @Operation(summary = "根据新闻大类id查询新闻")
    @GetMapping("/getbythemeid")
    public List<News> getByThemeId(@RequestParam Integer themeid) {
        UpdateWrapper<News> wrapper = new UpdateWrapper<>();
        wrapper.eq("themeid",themeid);
        return new News().selectList(wrapper);
    }

    @Operation(summary = "根据新闻id查询新闻")
    @GetMapping("/getbynewsid")
    public News getByNewsId(@RequestParam Integer newsid) {
        return new News().selectById(newsid);
    }


}
